package com.zhitong.money.mapper;

import com.zhitong.money.entity.Holiday;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface HolidayMapper {

    @Results(id = "holidayMap",value ={
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "startTime",property = "startTime"),
            @Result(column = "endTime",property = "endTime"),
            @Result(column = "description",property = "description"),
            @Result(column = "status",property = "status")
    } )
    @Select("select * from holiday")
    public List<Holiday> findAllHoliday();

    @ResultMap("holidayMap")
    @Select("select * from holiday where name=#{name}")
    public Holiday findHolidayByName(String name);

    @ResultMap("holidayMap")
    @Select("select * from holiday where id=#{id}")
    public Holiday findHolidayById(String id);

    @ResultMap("holidayMap")
    @Select({
            "<script>",
            "select * from holiday",
            "<where>",
            "<if test='name!=null and name != \"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='startTime != null'>and startTime = #{startTime}</if>",
            "<if test='endTime != null'>and endTime = #{endTime}</if>",
            "<if test='status !=null and status!=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<Holiday> findHolidayByMap(Map<String,Object> map);

    @Insert("insert into holiday values(#{id},#{name},#{startTime},#{endTime},#{description},#{status})")
    public void addHoliday(Holiday holiday);

    @Update({
            "<script>",
            "update holiday",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='name!=null and name!=\"\"'>name=#{name},</if>",
            "<if test='startTime!=null'>startTime=#{startTime},</if>",
            "<if test='endTime!=null'>endTime=#{endTime},</if>",
            "<if test='description!=null and description!=\"\"'>description=#{description},</if>",
            "<if test='status!=null and status!=\"\"'>status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateHoliday(Holiday holiday);

    @Delete("delete from holiday where id=#{id}")
    public void delHoliday(String id);

    @Delete({
            "<script>",
            "delete from holiday",
            "where id in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delHolidayByIds(List<String> ids);

}
